package com.xbongbong.paas.service.stage.impl;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.domain.entity.StageTimeoutRemindEntity;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.StageTypeEnum;
import com.xbongbong.paas.model.StageTimeoutRemindModel;
import com.xbongbong.paas.pojo.StageMessageContentPojo;
import com.xbongbong.paas.pojo.StageRemindPojo;
import com.xbongbong.paas.pojo.vo.StageGetVO;
import com.xbongbong.paas.service.stage.AbstractStageSettingsStrategy;
import com.xbongbong.paas.service.stage.StageSettingsStrategy;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.constant.StageProcessConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.CustomerStageErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.StageWorkEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 普通阶段设置策略
 * @author 李少杰
 * @version v1.0.0
 * @date 2021/11/22 13:43
 */
@Service("ordinaryStageSettingsStrategy")
public class OrdinaryStageSettingsStrategy extends AbstractStageSettingsStrategy implements StageSettingsStrategy {

    @Resource
    private StageTimeoutRemindModel stageTimeoutRemindModel;


    @Override
    public Integer type() {
        return StageTypeEnum.ORDINARY_STAGE.getType();
    }

    @Override
    public StageGetVO get(PaasStageEntity paasStageEntity, List<StageWorkEntity> stageWorkEntityList) throws XbbException {
        StageGetVO stageGetVO = wrapStageEntity(paasStageEntity);
        List<StageRemindPojo> stageRemind = getStageRemind(paasStageEntity.getCorpid(), paasStageEntity.getFormId(), paasStageEntity.getStageProcessId(), paasStageEntity.getId());
        stageGetVO.setStageRemindPojoList(stageRemind);
        return stageGetVO;
    }

    @Override
    public void check(List<PaasStageEntity> paasStageEntityList, Integer businessType) throws XbbException {
        if (CollectionsUtil.isEmpty(paasStageEntityList)) {
            throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238038, CustomerStageErrorCodeEnum.API_ERROR_238038.getMsg(), StageTypeEnum.ORDINARY_STAGE.getName(), StageTypeEnum.ORDINARY_STAGE.getName());
        }
        if (paasStageEntityList.size() > StageProcessConstant.ORDINART_STAGE_MAX_COUNT) {
            throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238062, CustomerStageErrorCodeEnum.API_ERROR_238062.getMsg(), StageProcessConstant.ORDINART_STAGE_MAX_COUNT, StageTypeEnum.ORDINARY_STAGE.getName());
        }
        PaasStageEntity lastStage = null;
        for (PaasStageEntity paasStageEntity : paasStageEntityList) {
            if (Objects.isNull(lastStage)) {
                if (Objects.isNull(paasStageEntity.getIntoProportion())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.STAGE_INTO_PROPORTION_NOT_NULL);
                }
                if (paasStageEntity.getIntoProportion() > paasStageEntity.getOutProportion()) {
                    throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238014);
                }
                lastStage = paasStageEntity;
                continue;
            }
            if (paasStageEntity.getOutProportion() < lastStage.getOutProportion()) {
                throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238014);
            }
            paasStageEntity.setIntoProportion(lastStage.getOutProportion());
            lastStage = paasStageEntity;
        }
    }

    /**
     * 获取阶段超时设置
     * @param corpid 公司id
     * @param formId 表单id
     * @param stageProcessId 阶段流程id
     * @param stageId 阶段id
     * @return pojoList
     */
     private List<StageRemindPojo> getStageRemind(String corpid, Long formId, Long stageProcessId, Long stageId) {
         Map<String, Object> params = new HashMap<>(5);
         params.put(StringConstant.CORPID, corpid);
         params.put(StringConstant.FORM_ID, formId);
         params.put(ParameterConstant.STAGE_PROCESS_ID, stageProcessId);
         params.put(ParameterConstant.STAGE_ID, stageId);
         params.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
         List<StageTimeoutRemindEntity> stageTimeoutRemindEntityList = stageTimeoutRemindModel.findEntitys(params);
        if (CollectionsUtil.isEmpty(stageTimeoutRemindEntityList)) {
            return null;
        }
        List<StageRemindPojo> stageRemindPojoList = new ArrayList<>(stageTimeoutRemindEntityList.size());
        for (StageTimeoutRemindEntity stageTimeoutRemindEntity : stageTimeoutRemindEntityList) {
            StageRemindPojo stageRemindPojo = new StageRemindPojo();
            stageRemindPojo.setId(stageTimeoutRemindEntity.getId());
            stageRemindPojo.setRemindType(stageTimeoutRemindEntity.getRemindType());
            stageRemindPojo.setRemindTime(stageTimeoutRemindEntity.getRemindTime());
            JSONArray remindUser = Objects.isNull(stageTimeoutRemindEntity.getRemindUser()) ? new JSONArray() : stageTimeoutRemindEntity.getRemindUser();
            stageRemindPojo.setRemindUser(remindUser.toJavaList(OptionalRangeEntity.class));
            JSONArray remindContent = Objects.isNull(stageTimeoutRemindEntity.getRemindContent()) ? new JSONArray() : stageTimeoutRemindEntity.getRemindContent();
            stageRemindPojo.setRemindContent(remindContent.toJavaList(StageMessageContentPojo.class));
            stageRemindPojoList.add(stageRemindPojo);
        }
        return stageRemindPojoList;
     }
}
